jQuery(function ($) {
    
    $('.populated-text').each(function () {
        populateText($(this));
    });    
});
var populateText = function (input){    
    input.data('blank-value', input.val());
    input.data('data-entered', false);
    input.addClass('grayed');
    input.keydown(keyDown);
    input.blur(blur);
    input.focus(focus);
    input.mousedown(mouse);
    input.mouseup(mouse);
}

$.fn.selectRange = function(start, end) {
    return this.each(function() {
        if (this.setSelectionRange) {
            this.focus();
            this.setSelectionRange(start, end);
        } else if (this.createTextRange) {
            var range = this.createTextRange();
            range.collapse(true);
            range.moveEnd('character', end);
            range.moveStart('character', start);
            range.select();
        }
    });
};

// Removes the defualt text on key press
function keyDown (e) {
    var input = $(e.target);
        
    if (input && !input.data('data-entered')) {
        input.data('data-entered', true);
        input.removeClass('grayed');
        input.val("");
    }
}
    
// Restore the default text if empty on blur
function blur(e) {
    var input = $(e.target);
        
    if (input && input.val() === "") {
        input.data('data-entered', false);
        input.addClass('grayed');
        input.val(input.data('blank-value'));
    }
}
    
// Sets the caret position to the start or end on focus
function focus (e) {
    var input = $(e.target), len;
        
    if (input) {
        if (!input.data('data-entered') ) {
            // focus at the start if data not entered
            //            input.selectRange(0,0);
            if( input.data('blank-value') == input.val()){
                //                if(input.hasClass("hasDatepicker") == false){
                input.val("");                  
                input.selectRange(0,0);  
            //                }
            }
        } else {
            // focus at the end if text is entered
            if( input.val() == ""){
                input.val(input.data('blank-value'));
            //                len = input.val().length;
            //                input.selectRange(len, len);
            }
        }
    }
}
   
// Sets the caret to the start if the empty text is displayed    
function mouse (e) {
    var input = $(e.target);
        
    if (input && !input.data('data-entered')) {
        if( input.data('blank-value') == input.val()){
            //            if(input.hasClass("hasDatepicker") == false){
            input.val("");           
            input.selectRange(0,0);         
        //            }
        }
    //        input.selectRange(0,0);
    }
}

function setDataEntered(container){
    container.find(".populated-text").each(function(){
        if($(this).val() != ""){
            $(this).data('data-entered', true);
            $(this).removeClass("grayed");
        }
    });
}